package com.ufo.store.management.ui.activity;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;

import com.ufo.store.management.BarApplication;
import com.ufo.store.management.R;
import com.ufo.store.management.database.DatabaseManager;
import com.ufo.store.management.entity.SaleDetailInfo;
import com.ufo.store.management.entity.StockInfo;
import com.ufo.store.management.utils.AnimationUtil;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by Administrator on 2015/10/10 0010.
 */
public class SaleDetailActivity extends BaseActivity {
    private RelativeLayout mProcessTipLy = null;
    private List<HashMap<String, Object>> mSaleDetailMapList = new ArrayList<HashMap<String, Object>>();
    ;
    private String mSaleBillno = null;
    private ListView mSaleDetailListView = null;
    private SimpleAdapter mAdapter = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initView();
        Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar);
        toolbar.setTitle(mSaleBillno);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_back_selector);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                mSaleBillno = bundle.getString("Billno");
                //SerializableList p = (SerializableList) bundle.getSerializable("SaleDetail");
                //mSaleDetailMapList = p.getSaleDetailMapList();
            }
        }
    }

    private void initView() {
        setContentView(R.layout.activity_sale_detail);
        mProcessTipLy = (RelativeLayout) findViewById(R.id.progress_tip_ly);
        mSaleDetailListView = (ListView) findViewById(R.id.saledetail_listview);
        mAdapter = new SimpleAdapter(SaleDetailActivity.this, mSaleDetailMapList, R.layout.sale_detail_listitem, new String[]{"name", "quantity", "price", "total"}, new int[]{R.id.name, R.id.quantity, R.id.price, R.id.total});
        mSaleDetailListView.setAdapter(mAdapter);
        AnimationUtil.setTopToBottomAnimation(mSaleDetailListView, 400, 0.1f);
        new GetSaleDetailInfoTask().execute(mSaleBillno);
    }

    private class GetSaleDetailInfoTask extends AsyncTask<String, Void, List<HashMap<String, Object>>> {
        public GetSaleDetailInfoTask() {
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

        }

        @Override
        protected List<HashMap<String, Object>> doInBackground(String... params) {
            String billno = params[0];
            DecimalFormat decimalFormat = new DecimalFormat("0.0");
            List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
            List<SaleDetailInfo> saleDetailList = new ArrayList<>();
            if (BarApplication.getInstance().mDatabaseIsSqlServer) {
                saleDetailList = DatabaseManager.getSaleDetailInfoFromSqlServer(SaleDetailActivity.this, 0, billno);
            } else {
                saleDetailList = DatabaseManager.getSaleDetailInfoFromLocal(SaleDetailActivity.this, 0, billno);
            }
            for (SaleDetailInfo saleDetailInfo : saleDetailList) {
                HashMap<String, Object> item = new HashMap<String, Object>();
                List<StockInfo> result = new ArrayList<>();
                if (BarApplication.getInstance().mDatabaseIsSqlServer)
                    result = DatabaseManager.getStockInfoListFromSqlServer(SaleDetailActivity.this, 0, -1, -1, 5, -1, saleDetailInfo.saleDetailStockid);
                else
                    result = DatabaseManager.getStockInfoListFromLocal(SaleDetailActivity.this, 0, -1, 5, -1, saleDetailInfo.saleDetailStockid);
                if (result != null && result.size() != 0) {
                    item.put("name", result.get(0).stockName);
                } else {
                    item.put("name", saleDetailInfo.saleDetailStockid);
                }
                item.put("quantity", decimalFormat.format(saleDetailInfo.saleDetailQuantity));
                item.put("price", decimalFormat.format(saleDetailInfo.saleDetailPrice));
                item.put("total", decimalFormat.format(saleDetailInfo.saleDetailQuantity * saleDetailInfo.saleDetailPrice));
                data.add(item);
            }

            return data;
        }

        @Override
        protected void onPostExecute(List<HashMap<String, Object>> result) {
            super.onPostExecute(result);
            if (result != null)
                mSaleDetailMapList = result;

            mProcessTipLy.setVisibility(View.GONE);
            mSaleDetailListView.setVisibility(View.VISIBLE);

            mAdapter = new SimpleAdapter(SaleDetailActivity.this, mSaleDetailMapList, R.layout.sale_detail_listitem, new String[]{"name", "quantity", "price", "total"}, new int[]{R.id.name, R.id.quantity, R.id.price, R.id.total});
            mSaleDetailListView.setAdapter(mAdapter);
        }
    }

}
